<?php

namespace App\Http\Controllers\Admin;

use App\Models\Cate;
use App\Models\Member;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Article as ArticleModel;

class Article extends Controller
{
    //文章列表
    public function all()
    {
        $articles = ArticleModel::with('members', 'cates')->orderBy('is_top')->orderBy('status')->orderBy('created_at', 'desc')->paginate(10);
        $viewData = [
            'articles' => $articles
        ];
        return view('admin.article.list', $viewData);
    }

    //上架下架操作
    public function up()
    {
        $articleInfo = ArticleModel::find(request('id'));
        $articleInfo->status = request('status') ? '0' : 1;
        $result = $articleInfo->save();
        if ($result) {
            $msg = [
                'code' => 1,
                'msg' => '操作成功',
                'url' => url('admin/articlelist')
            ];
        }else {
            $msg = [
                'code' => 0,
                'msg' => '操作失败'
            ];
        }
        return $msg;
    }

    //推荐操作
    public function top()
    {
        $articleInfo = ArticleModel::find(request('id'));
        $articleInfo->is_top = request('is_top') ? '0' : 1;
        $result = $articleInfo->save();
        if ($result) {
            $msg = [
                'code' => 1,
                'msg' => '操作成功',
                'url' => url('admin/articlelist')
            ];
        }else {
            $msg = [
                'code' => 0,
                'msg' => '操作失败'
            ];
        }
        return $msg;
    }

    //文章添加
    public function add()
    {
        if (request()->isMethod('post')) {
            $data = request()->only(['title', 'tags', 'member_id', 'cate_id', 'desc', 'content']);
            $result = (new ArticleModel)->add($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg' => '文章添加成功',
                    'url' => url('admin/articlelist')
                ];
            }else {
                $msg = [
                    'code' => 0,
                    'msg' => $result
                ];
            }
            return $msg;
        }
        $members = Member::all();
        $cates = Cate::all();
        $viewData = [
            'members' => $members,
            'cates' => $cates
        ];
        return view('admin.article.add', $viewData);
    }

    //文章编辑
    public function edit()
    {
        if (request()->isMethod('post')) {
            $data = request()->only(['id', 'title', 'tags', 'member_id', 'cate_id', 'desc', 'content']);
            $result = (new ArticleModel)->edit($data);
            if ($result == 1) {
                $msg = [
                    'code' => 1,
                    'msg' => '文章修改成功',
                    'url' => url('admin/articlelist')
                ];
            }else {
                $msg = [
                    'code' => 0,
                    'msg' => $result
                ];
            }
            return $msg;
        }
        $articleInfo = ArticleModel::find(request('id'));
        $members = Member::all();
        $cates = Cate::all();
        $viewData = [
            'articleInfo' => $articleInfo,
            'members' => $members,
            'cates' => $cates
        ];
        return view('admin.article.edit', $viewData);
    }

    //文章删除
    public function del()
    {
        $articleInfo = ArticleModel::with('comments')->find(request('id'));
        foreach($articleInfo->comments as $vo) {
            $vo->delete();
        }
        $result = $articleInfo->delete();
        if ($result) {
            $msg = [
                'code' => 1,
                'msg' => '删除成功',
                'url' => url('admin/articlelist')
            ];
        }else {
            $msg = [
                'code' => 0,
                'msg' => '删除失败'
            ];
        }
        return $msg;
    }
}
